package com.orcamento.entity.report;

import java.util.ArrayList;
import java.util.List;

import com.orcamento.entity.Category;
import com.orcamento.entity.Event;
import com.orcamento.entity.Fund;
import com.orcamento.entity.Gain;
import com.orcamento.entity.Spent;
import com.orcamento.entity.enums.Month;

public class ReportBuilder {
	
	public static final Integer OVERVIEW_REPORT = 1;
	public static final Integer GENERAL_BALANCE_REPORT = 2;
	public static final Integer CATEGORY_REPORT = 3;
	public static final Integer EVENT_REPORT = 4;
	public static final Integer HERITAGE_REPORT = 5;
	
	protected List<Gain> gains = new ArrayList<Gain>();
	protected List<Spent> spents = new ArrayList<Spent>();
	protected List<Category> categories = new ArrayList<Category>();
	protected List<Fund> funds = new ArrayList<Fund>();
	protected List<Event> events = new ArrayList<Event>();
	private Integer type;
	
	public static ReportBuilder newInstance() {
		return new ReportBuilder();
	}
	
	private ReportBuilder() { }
	
	public ReportBuilder withGains(List<Gain> gains) {
		this.gains = gains;
		return this;
	}
	
	public ReportBuilder withSpents(List<Spent> spents) {
		this.spents = spents;
		return this;
	}
	
	public ReportBuilder withCategories(List<Category> categories) {
		this.categories = categories;
		return this;
	}
	
	public ReportBuilder withFunds(List<Fund> funds) {
		this.funds = funds;
		return this;
	}
	
	public ReportBuilder withEvents(List<Event> events) {
		this.events = events;
		return this;
	}

	public ReportBuilder withType(Integer type) {
		this.type = type;
		return this;
	}
	
	public Report build(Integer year, Month month) {
		if( ReportBuilder.OVERVIEW_REPORT.equals( type ) ) {
			return buildOverviewReport(year, month);
		}
		
		if( ReportBuilder.CATEGORY_REPORT.equals(type) ) {
			return buildCategoryReport(year, month);
		}
		
		if( ReportBuilder.EVENT_REPORT.equals(type) ) {
			return buildEventReport(year, month);
		}
		
		if( ReportBuilder.HERITAGE_REPORT.equals(type) ) {
			return buildHeritageReport(year, month);
		}
		
		return buildGeneralBalanceReport(year, month); 
	}

	private Report buildHeritageReport(Integer year, Month month) {
		HeritageReport heritageReport = new HeritageReport(month, year);
		heritageReport.setFunds(funds);
		heritageReport.generate();
		return heritageReport;
	}

	private Report buildEventReport(Integer year, Month month) {
		EventReport eventReport = new EventReport(month, year);
		eventReport.setEvents(events);
		eventReport.generate();
		return eventReport;
	}

	private Report buildCategoryReport(Integer year, Month month) {
		CategoryReport categoryReport = new CategoryReport(month, year);
		categoryReport.setCategories(categories);
		categoryReport.generate();
		return categoryReport;
	}

	private Report buildGeneralBalanceReport(Integer year, Month month) {
		GeneralBalanceReport generalBalanceReport = new GeneralBalanceReport(month, year);
		generalBalanceReport.setGains( gains );
		generalBalanceReport.setSpents( spents );
		generalBalanceReport.setEvents( events );
		generalBalanceReport.setCategories( categories );
		generalBalanceReport.generate();
		return generalBalanceReport;
	}

	private Report buildOverviewReport(Integer year, Month month) {
		OverviewReport overviewReport = new OverviewReport(month, year);
		overviewReport.setGains( gains );
		overviewReport.setSpents( spents );
		overviewReport.setFunds( funds );
		overviewReport.generate();
		return overviewReport;
	}
}
